#include <X11/Xlib.h>
#include <assert.h>
#include <stdio.h>
#include "frontend.h"
#include "backend.h"
#include "types.h"

/* TODO: grab the keyboard with XGrabKeyboard(dpy, DefaultRootWindow(dpy), GrabModeAsync
 */

/* Simple logger, creates a window and then listens on it.
 */
int main(int argc, char *argv[])
{
	Display *dpy;
	Window w;

	dpy = XOpenDisplay(0);
	assert(dpy);

	w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
				200, 100, 0, 0,	~0);

	XSelectInput(dpy, w, KeyPressMask | StructureNotifyMask);
	XMapWindow(dpy, w);
	for (;;) {
		XEvent e;
		XNextEvent(dpy, &e);
		if (e.type == MapNotify)
			break;
	}

	XStoreName(dpy, w, "Keystroke Window");
	XFlush(dpy);

	for (;;) {
		XKeyPressedEvent e;
		XNextEvent(dpy, (XEvent *)&e);

		if ((e.type == KeyPress)
		    && (e.send_event == False))
			key_struck(e.keycode);
	}

	return 0;
}
